

package yf.mall.service.impl.app;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.vo.up.AppBehaviorUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.vo.down.AppBehaviorDown;

import java.util.Date;
import java.util.List;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.AppBehaviorDao;
import yf.mall.entity.AppBehaviorEntity;
import yf.mall.vo.AppBehaviorVo;
import yf.mall.service.app.AppBehaviorService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 *
 *
 * @date Fri Jul 28 07:58:50 CST 2023
 */
@Service
public class AppBehaviorServiceImpl extends BaseServiceImpl<AppBehaviorEntity> implements AppBehaviorService {

    private AppBehaviorDao dao;


    @Autowired
    public void setDao(AppBehaviorDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppBehaviorVo> findVo(AppBehaviorVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppBehaviorVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppBehaviorVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppBehaviorVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppBehaviorVo> list) {
        try {
            List<AppBehaviorDown> datas = list.stream().map(item -> new AppBehaviorDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppBehaviorDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppBehaviorUp.class, new UploadDataListener()).sheet().doRead();
    }


    class UploadDataListener extends AnalysisEventListener<AppBehaviorUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppBehaviorUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/
    @Override
    public void record(Long userId, Long refId, Integer type, String remark, Integer minInterval) {

        List<AppBehaviorEntity> behaviors = dao.findBehavior(userId, refId, type, DateUtils.addSeconds(new Date(), -1 * minInterval));

        AppBehaviorEntity lastBehavior = behaviors.stream().findFirst().orElse(null);

        if (lastBehavior == null) {
            AppBehaviorEntity entity = new AppBehaviorEntity();
            entity.setType(type);
            entity.setRefId(refId);
            entity.setRemark(remark);
            entity.setCreatorId(userId);
            entity.setCreatorName("登录用户：" + userId);
            entity.setCreateTime(new Date());
            dao.save(entity);
        } else {
            // 持续进行的行为，更新最新行为即可
            AppBehaviorEntity entity = new AppBehaviorEntity();
            entity.setId(lastBehavior.getId());
            entity.setType(type);
            entity.setRefId(refId);
            entity.setRemark(remark);
            entity.setCreatorId(userId);
            dao.update(entity);
        }
    }


}
